package com.ms3ds.mineInfo.controller;

import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ms3ds.common.core.domain.R;
import com.ms3ds.common.core.utils.DateUtils;
import com.ms3ds.mineInfo.api.domain.Team;
import com.ms3ds.mineInfo.service.ITeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ms3ds.common.log.annotation.Log;
import com.ms3ds.common.log.enums.BusinessType;
import com.ms3ds.mineInfo.api.domain.Task;
import com.ms3ds.mineInfo.service.ITaskService;
import com.ms3ds.common.core.web.controller.BaseController;
import com.ms3ds.common.core.web.domain.AjaxResult;
import com.ms3ds.common.core.utils.poi.ExcelUtil;
import com.ms3ds.common.core.web.page.TableDataInfo;

/**
 * 开采任务Controller
 * 
 * @author zlw
 * @date 2025-09-19
 */
@RestController
@RequestMapping("/task")
public class TaskController extends BaseController
{
    @Autowired
    private ITaskService taskService;

    @Autowired
    private ITeamService teamService;

    /**
     * 查询开采任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Task task)
    {
        startPage();
        List<Task> list = taskService.selectTaskList(task);
        return getDataTable(list);
    }

    @GetMapping("/getSystemPreviewTask")
    public AjaxResult getSystemPreviewTask(Long mineId){
        return success(taskService.getSystemPreviewTask(mineId));
    }

    @GetMapping("/getTaskByTeamIdForRemote")
    public R<List<Task>> getTaskByTeamIdForRemote(Long teamId)
    {
        Task task = new Task();
        task.setTeamId(teamId);
        List<Task> tasks = taskService.selectTaskList(task);
        return R.ok(tasks);
    }

    /**
     * 通过生产队id查询年度开采任务
     * @param teamId
     * @return
     */
    @GetMapping("/getYearTaskByTeamIdForRemote")
    public R<Task> getYearTaskByTeamIdForRemote(Long teamId){
        Task task = new Task();
        task.setTeamId(teamId);
        task.setTaskType(1L);//年度任务
        //设置查询时间
        Map<String,Object> map = new LinkedHashMap<>();
        Date[] yesterdayRange = DateUtils.getThisYearRange();
        map.put("beginStartTime",DateUtils.paraseString(yesterdayRange[0]));
        map.put("endStartTime",DateUtils.paraseString(yesterdayRange[1]));
        task.setParams(map);
        List<Task> tasks = taskService.selectTaskList(task);
        return R.ok(tasks.isEmpty()?null:tasks.get(0));
    }

    Float taskMaxPlan = 20000f;
    /**
     * 给所有生产队生成月度任务
     * @return
     */
    @GetMapping("/generateMonthTask")
    public R<Boolean> generateMonthTask(){
        //先获取所有生产队
        List<Team> teams = teamService.selectTeamList(new Team());
        for(Team team:teams){
            Task searchTask = new Task();
            searchTask.setTeamId(team.getId());
            searchTask.setTaskType(3L);
            Map<String,Object> map = new HashMap<>();
            Date nowDate = DateUtils.getNowDate();
            map.put("beginStartTime",DateUtils.paraseString(nowDate));
            map.put("endStartTime",DateUtils.paraseString(DateUtils.addMonths(nowDate,1)));
            searchTask.setParams(map);
            List<Task> tasks = taskService.selectTaskList(searchTask);
            if(tasks==null||tasks.isEmpty()){//如果未设置月度任务，则进行生成
                Task task = new Task();
                task.setTeamId(team.getId());
                task.setTaskType(3L);
                task.setStartTime(nowDate);
                float rate = (float) (0.8 + Math.random() * (1.2 - 0.8));//假设范围在0.8-1.2之间
                task.setPlanMining(taskMaxPlan*rate);
                taskService.insertTask(task);
            }
        }
        return R.ok(true);
    }

    /**
     * 导出开采任务列表
     */
    @Log(title = "开采任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Task task)
    {
        List<Task> list = taskService.selectTaskList(task);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task.class);
        util.exportExcel(response, list, "开采任务数据");
    }

    /**
     * 获取开采任务详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(taskService.selectTaskById(id));
    }

    /**
     * 新增开采任务
     */
    @Log(title = "开采任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Task task)
    {
        return toAjax(taskService.insertTask(task));
    }

    /**
     * 修改开采任务
     */
    @Log(title = "开采任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Task task)
    {
        return toAjax(taskService.updateTask(task));
    }

    @PutMapping("/editTaskForRemote")
    public R<Boolean> editTaskForRemote(@RequestBody Task task){
        return R.ok(taskService.updateTask(task)==1);
    }

    /**
     * 删除开采任务
     */
    @Log(title = "开采任务", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(taskService.deleteTaskByIds(ids));
    }
}
